<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将文件截断到给定的长度</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ftruncate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ftruncate</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">ftruncate</span> &mdash; <span class="dc-title">将文件截断到给定的长度</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.ftruncate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ftruncate</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   接受文件指针 <code class="parameter">handle</code> 作为参数，并将文件大小截取为
   <code class="parameter">size</code>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.ftruncate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">
       文件指针。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        The <code class="parameter">handle</code> must be open for writing.
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<code class="parameter">size</code></dt>

     <dd>

      <p class="para">
       The size to truncate to.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        If <code class="parameter">size</code> is larger than the file then the file
        is extended with null bytes.
       </p>
       <p class="para">
        If <code class="parameter">size</code> is smaller than the file then the file
        is truncated to that size.
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.ftruncate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.ftruncate-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.3</td>
       <td>
        在 PHP 4.3.3 之前，<span class="function"><strong>ftruncate()</strong></span> 在成功时返回一个
    <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 值 1，而不是 <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 的 <strong><code>TRUE</code></strong>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.ftruncate-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2809">
    <p><strong>Example #1 File truncation example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'lorem_ipsum.txt'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ftruncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.ftruncate-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The file pointer is <em class="emphasis">not</em> changed.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.ftruncate-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="fseek.html" class="function" rel="rdfs-seeAlso">fseek()</a> - 在文件指针中定位</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>